package cn.itmantzy.system.controller;

import cn.itmantzy.basic.util.AjaxResult;
import cn.itmantzy.basic.util.PageList;
import cn.itmantzy.system.domain.DicDetail;
import cn.itmantzy.system.query.DicDetailQuery;
import cn.itmantzy.system.service.IDicDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DicDetailController {
    @Autowired
    private IDicDetailService dicDetailService;

    @GetMapping("/list")
    public List<DicDetail> findAll(){
        return dicDetailService.queryAll();
    }

    @PostMapping("/page")
    public PageList<DicDetail> findByQuery(@RequestBody DicDetailQuery query){
        return dicDetailService.queryPage(query);
    }

    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody DicDetail dicDetail){
        try {
            if (dicDetail.getId() == null){
                dicDetailService.add(dicDetail);
            }else {
                dicDetailService.update(dicDetail);
            }
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后再试！！！");
        }
    }

    @DeleteMapping("{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dicDetailService.remove(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后再试！！！");
        }
    }

}
